#pragma once
#include <windows.h>
#include "MemDC.h"
#include "WinAPI.h"
 
// Structure associated with menu items  
//typedef struct tagMYITEM 
//{ 
//    HFONT hfont; 
//    int cchItemText; 
//    LPTSTR szItemText; 
//} MYITEM;

// the first 2 items are actually used for width and height of the icons
typedef struct tagButtonRef
{
	int uID;
	int icon;
} BUTTON_REF, *LPBUTTONREF;
 
#define CCH_MAXITEMTEXT		256

#define MFT_TITLE			0x40000000

class CMenu
{
public:
	CMenu();
	~CMenu(void);

	operator HMENU() const { return hMMenu; }

	BOOL OnCreate(HWND hWindow);
	void OnInitMenuPopup(HMENU hMenu/*, UINT nIndex*/, BOOL bSysMenu);
	void OnUnInitMenuPopup(HMENU _hMenu);
	void OnDestroy();
	void OnDrawItem(LPDRAWITEMSTRUCT lpdis);
	void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpmis);
	void OnNCPaint();

	//void Push(HMENU hMenu) { this->hMenu=hMenu; hMenuStack.push_back(hMenu); }
	//void Pop() { hMenuStack.pop_back(); hMenu=hMenuStack.size()==0 ? NULL : hMenuStack[hMenuStack.size()-1]; }

	//static HFONT CreateMenuItemFont(UINT uID);
	// dynamic creation
	inline HMENU CreatePopupMenu() { return hMMenu=::CreatePopupMenu(); }
	inline BOOL AppendMenu(UINT uFlags, UINT_PTR uIDNewItem, LPCWSTR lpNewItem) { return ::AppendMenu(*this,uFlags,uIDNewItem,lpNewItem); }
	inline BOOL RemoveMenu(UINT uPosition, UINT uFlags) { return ::RemoveMenu(*this,uPosition,uFlags); }

	void LoadBitmap(LPCTSTR lpBitmapName);
	void SetButtonRef(LPBUTTONREF ref, int count);
	void SplitMenuText(LPTSTR menuItemText, LPTSTR &menuText, LPTSTR &acceleratorText);

protected:
	HWND hWnd;
	//HMENU hMenu;
	HMENU hMMenu;
	HFONT hFont;
	CBitmap hBitmap;

	struct MenuItem
	{
		//int iImageIdx;
		GString cText;
		UINT type, state;
		int iCmd;
	};
	int item_cnt;
	//MenuItem **item_stack;
	std::vector<MenuItem*> item_stack;

	MenuItem* AddItem(LPCTSTR string, const int cmd, UINT type, UINT state);

	//Draw
	LPBUTTONREF button_ref;
	int button_cnt/*, iRecourse*/;
};
